package xj.graph2d.app.prop;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import xj.graph2d.Command;
import xj.graph2d.DrawAttr;
import xj.graph2d.DrawCanvas;
import xj.graph2d.DrawShape;
import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.SimpleDrawView;
import xj.graph2d.app.DrawApp;
import xj.graph2d.command.SetAttrCommand;

public class CapSelectionListener 
  extends AttrListener 
  implements ActionListener {

  public void init(CapIcon icon) {
    this.icon = icon;
    SimpleDrawDocument doc = main.getDoc();
    SimpleDrawView view = main.getView();
    attr = view.getDrawAttr();
  }

  @Override
  protected void init() {
    SimpleDrawDocument doc = main.getDoc();
    SimpleDrawView view = main.getView();
    attr = view.getDrawAttr();
    Object ic = DrawApp.getIcon("capIcon");
    if (ic instanceof CapIcon) {
      icon = (CapIcon) ic;
    }
  }

  public void actionPerformed(ActionEvent e) {
    SimpleDrawDocument doc = main.getDoc();
    SimpleDrawView view = main.getView();
    attr = view.getDrawAttr();
    if (attr != null && popup == null) {
      initPopup();
    }
    Object source = e.getSource();
    if (source instanceof AbstractButton) {
      comp = (AbstractButton) source;
    }
    if (popup != null && comp != null) {
      int h = comp.getHeight();
      popup.show(comp, 0, h);
    }

    if (comp != null) {
      comp.setSelected(false);
    }
  }

  class CapAction implements ActionListener {

    public void actionPerformed(ActionEvent e) {
      Object source = e.getSource();
      if (source instanceof JMenuItem) {
	JMenuItem button = (JMenuItem) source;
	Container p = button.getParent();
	p.setVisible(false);
	CapIcon ic = (CapIcon) button.getIcon();
	int cap = ic.getCapType();
	if (icon != null && comp != null) {
	  icon.setCapType(cap);
	  comp.repaint();
	}

	if (updateSystemAttr) {
	  // update system attribute
	  attr.setLineCap(cap);
	}

	// update current shape
	SimpleDrawDocument doc = main.getDoc();
	SimpleDrawView view = main.getView();
	if (doc != null) {
	  DrawShape curShape = doc.getSelectedShape();
	  if (curShape != null) {
	    DrawAttr curAttr = curShape.getDrawAttr();
	    if (curAttr != null) {
	      Command cmd = new SetAttrCommand(view, doc, curShape);
	      doc.addCommand(cmd);

	      curAttr.setLineCap(cap);
	      curShape.calculateBounds();
	      DrawCanvas canvas = view.getCanvas();
	      canvas.repaintShape(curShape);
	    }
	  }
	}
      }
    }
  }

  protected void initPopup() {
    ActionListener act = new CapAction();

    popup = new JPopupMenu();
    JMenuItem mi;
    for (int i = DrawAttr.LINE_CAP_BUTT; i <= DrawAttr.LINE_CAP_SQUARE; i++) {
      CapIcon icon = new CapIcon();
      icon.setCapType(i);
      String text = null;
      switch (i) { 
      case DrawAttr.LINE_CAP_BUTT:
	text = "Butt cap";
	break; 
      case DrawAttr.LINE_CAP_ROUND:
	text = "Round cap";
	break; 
      case DrawAttr.LINE_CAP_SQUARE:
	text = "Square cap";
	break; 
      }
      mi = new JMenuItem();
      mi.setText(text);
      mi.setIcon(icon);
      //mi.setMargin(new Insets(0, 0, 0, 0));
      mi.addActionListener(act);
      popup.add(mi);
    }
  }

  protected CapIcon icon = null;

  protected DrawAttr attr = null;

  protected JPopupMenu popup = null;

  protected AbstractButton comp = null;

}
